home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Documentation / Books / Learn Java on the Macintosh / Learn Java Projects / 12.02 - arrays / ArrayApplet.java < prev    next >
Text File  |  1996-04-22  |  1KB  |  41 lines

  1. /* -------------------------------------------------------------
  2. This applet displays your fortune whenever you resize the applet.
  3.  
  4. Java's classes: Applet    (applet)
  5.                 Graphics  (awt)      used for drawing
  6.                 Math      (lang)     to find the absoluate value
  7.                 Date      (util)     gets the current date
  8.                 Random    (util)     finds a random number
  9.  
  10. Custom classes: ArrayApplet
  11.  
  12. ------------------------------------------------------------- */
  13. import java.awt.Graphics;
  14. import java.util.Date;
  15. import java.util.Random;
  16.  
  17. public class ArrayApplet extends java.applet.Applet {
  18.    int      numStrings = 5;
  19.    String[] paintStrings;
  20.    Random   r;
  21.  
  22.    public void init() {
  23.       Date d = new Date();         // today's date
  24.       r = new Random(d.getTime()); // seed with milliseconds since 1970
  25.       
  26.       paintStrings = new String[numStrings];
  27.       paintStrings[0]  = new String("Look for opportunities");
  28.       paintStrings[1]  = new String("Take chances");
  29.       paintStrings[2]  = new String("Beware of tricks");
  30.       paintStrings[3]  = new String("Take the day off");
  31.       paintStrings[4]  = new String("Smell the roses");
  32.    }
  33.  
  34.    public void paint(Graphics g) {
  35.  
  36.       int index = r.nextInt() % numStrings;
  37.       index = Math.abs(index);
  38.       g.drawString(paintStrings[index], 50, 25);
  39.       
  40.    }
  41. }